var gulp = require('gulp');
var shell = require('gulp-shell');

var nginx_server_conf_path = '/etc/nginx/conf.d';
var nginx_reload_cmd = 'sudo systemctl reload nginx';
var nginx_server_conf_path_mac = '/usr/local/etc/nginx/conf.d';
var nginx_reload_cmd_mac = 'sudo brew services restart nginx';

gulp.task('default', shell.task([
  'sudo mkdir -p /srv/docker/mocks',
  'sudo cp -R . /srv/docker/mocks',
  'docker-compose up -d',
]));

gulp.task('check', shell.task([
  'npm start',
]));

gulp.task('pull', shell.task([
  'cd mocks && git reset --hard && git pull',
]));

gulp.task('deploy', shell.task([
  'docker stop em_web_1',

  'if [ "Linux" = $(uname) ]; then sudo cp src/*.conf '
  + nginx_server_conf_path + '; else sudo mkdir -p '
  + nginx_server_conf_path_mac + '; sudo cp src/*.conf '
  + nginx_server_conf_path_mac + '; fi',

  'if [ "Linux" = $(uname) ]; then ' + nginx_reload_cmd
  + '; else echo "Please make sure NGINX \\`include default.d/*.conf;\\` in server part, and work for port 80!"; '
  + nginx_reload_cmd_mac + '; fi',

  'sudo rm -rf /srv/docker/mocks',
  'sudo mkdir -p /srv/docker/mocks',
  'sudo cp -R . /srv/docker/mocks',
  'docker start em_web_1',
]));

gulp.task('update', gulp.series(['pull', 'deploy']));

